/**
 * 
 */
package com.seguriapp.dao;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.net.Uri;
import android.util.Log;

import com.seguriapp.entities.CrimeCategoryEntity;
import com.seguriapp.providers.SeguriappProvider;
import com.seguriapp.util.Constants;

/**
 * @author pjohnson
 * 
 */
public class CrimeCategoryDao {

	public static final String TABLE_NAME = "CrimeCategory";
	public static final String COL_ID = "_id";
	public static final String COL_CID = "idCategory";
	public static final String COL_NAME = "name";
	public static final String COL_SELECTED = "selected";
	public static final String COL_DESCRIPTION = "description";

	// Content Provider constants
	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ SeguriappProvider.AUTHORITY + "/" + TABLE_NAME);
	public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.seguriapp.crimecategories";

	// Create the table
	protected static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + COL_ID + " " + Constants.INTEGER
			+ " PRIMARY KEY," + COL_CID + " " + Constants.INTEGER + ", "
			+ COL_SELECTED + " " + Constants.BOOLEAN + ", " + COL_NAME + " "
			+ Constants.TEXT + ", " + COL_DESCRIPTION + " " + Constants.TEXT
			+ ")";

	public static ContentValues contentValuesToInsert(final JSONObject category) {
		final ContentValues values = new ContentValues();
		try {
			values.put(COL_CID, category.getInt("id"));
			values.put(COL_NAME, category.getString("name"));
			values.put(COL_SELECTED, 1);
			values.put(COL_DESCRIPTION, category.getString("description"));
		} catch (JSONException e) {
			Log.e("error Json", e.getMessage());
		}
		return values;
	}

	public static ContentValues contentValuesToUpdate(
			final CrimeCategoryEntity category) {
		final ContentValues values = new ContentValues();
		values.put(COL_CID, category.getIdCategory());
		values.put(COL_SELECTED, category.isSelected());
		return values;
	}
}
